package nimble.pay.trade.server.framework;

import nimble.pay.trade.specification.TradeObject;
import org.springframework.beans.factory.BeanFactory;

import java.util.ArrayList;
import java.util.List;

/** 交易处理链
 * @author max woods
 * @date 2018/8/7
 */

public abstract class AbstractTradeProcessChain implements TradeProcessor{

  /**
   * 交易处理器列表
   */
  private final List<TradeProcessor> tradeProcessors=new ArrayList<>();

  protected BeanFactory beanFactory;

  /**
   * 初始化处理链
   * @param tradeProcessors
   */
  protected abstract void initChain(List<TradeProcessor> tradeProcessors);

  public AbstractTradeProcessChain(BeanFactory beanFactory)
  {
    this.beanFactory=beanFactory;
    initChain(tradeProcessors);
  }

  /**
   * 调用处理链
   * @param request
   * @param response
   * @return
   */
  @Override
  public boolean processTrade(TradeObject request, TradeObject response) {
    for (TradeProcessor tradeProcessor : tradeProcessors) {
      if(!tradeProcessor.processTrade(request, response))
      {
        return false;
      }
    }
    return true;
  }
}
